/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.nilai;

import com.ivanbiz.injection.CallCtx;
import com.ivanbiz.kelas.AccountKelas;
import com.ivanbiz.kelas.AccountKelasService;
import com.ivanbiz.matapelajaran.MataPelajaranService;
import com.ivanbiz.properties.SISBeanConstants;
import com.ivanbiz.properties.SISConstants;
import com.ivanbiz.properties.SISScreenConstants;
import com.ivanbiz.tools.IvanBiz;
import com.ivanbiz.user.User;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

/**
 *
 * @author Ivan
 */
public class NilaiAction extends IvanBiz {

    ActionMessages actionMessages = new ActionMessages();

    public ActionForward nilaiList(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        NilaiKriteriaService nilaiKriteriaService = (NilaiKriteriaService) CallCtx.getContext(request).getBean(SISBeanConstants.NILAI_KRITERIA_SERVICE);
        try {
            List listNilaiKriteria = nilaiKriteriaService.loadAll();
            request.setAttribute(SISConstants.LIST_NILAI_KRITERIA, listNilaiKriteria);
        } catch (Exception ex) {
            Logger.getLogger(NilaiAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.NILAI_LIST);
    }

    public ActionForward nilaiKriteriaList(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        NilaiKriteriaService nilaiKriteriaService = (NilaiKriteriaService) CallCtx.getContext(request).getBean(SISBeanConstants.NILAI_KRITERIA_SERVICE);
        try {
            List listNilaiKriteria = nilaiKriteriaService.loadAll();
            request.setAttribute(SISConstants.LIST_NILAI_KRITERIA, listNilaiKriteria);
        } catch (Exception ex) {
            Logger.getLogger(NilaiAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.NILAI_KRITERIA_LIST);
    }

    public ActionForward nilaiKriteriaAdd(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        NilaiKriteriaService nilaiKriteriaService = (NilaiKriteriaService) CallCtx.getContext(request).getBean(SISBeanConstants.NILAI_KRITERIA_SERVICE);
        NilaiForm nilaiForm = (NilaiForm) form;
        HttpSession session = request.getSession(true);
        User user = (User) session.getAttribute(SISConstants.USER );
        try {
            NilaiKriteria nilaiKriteria = new NilaiKriteria();
            nilaiKriteria.setNama(nilaiForm.getNamaKriteria());
            nilaiKriteria.setKeterangan(nilaiForm.getKeteranganKriteria());
            nilaiKriteria.setCreatedBy(user.getNama());
            nilaiKriteria.setCreatedDt(new Date());
            nilaiKriteriaService.saveNilaiKriteria(nilaiKriteria);
            nilaiForm.reset(mapping, request);
        } catch (Exception ex) {
            Logger.getLogger(NilaiAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.NILAI_KRITERIA);
    }

    public ActionForward nilaiKriteriaDelete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        NilaiKriteriaService nilaiKriteriaService = (NilaiKriteriaService) CallCtx.getContext(request).getBean(SISBeanConstants.NILAI_KRITERIA_SERVICE);
        try {
            NilaiKriteria nilaiKriteria = nilaiKriteriaService.loadById(Long.parseLong(request.getParameter(SISConstants.ID)));
            nilaiKriteriaService.deleteNilaiKriteria(nilaiKriteria);
        } catch (Exception ex) {
            Logger.getLogger(NilaiAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.NILAI_KRITERIA);
    }

    public ActionForward penilaianList(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        PenilaianService penilaianService = (PenilaianService) CallCtx.getContext(request).getBean(SISBeanConstants.PENILAIAN_SERVICE);
        NilaiKriteriaService nilaiKriteriaService = (NilaiKriteriaService) CallCtx.getContext(request).getBean(SISBeanConstants.NILAI_KRITERIA_SERVICE);
        AccountKelasService accountKelasService = (AccountKelasService) CallCtx.getContext(request).getBean(SISBeanConstants.ACCOUNT_KELAS_SERVICE);
        MataPelajaranService mataPelajaranService = (MataPelajaranService) CallCtx.getContext(request).getBean(SISBeanConstants.MATA_PELAJARAN_SERVICE);
        try {
            List listPenilaian = penilaianService.loadAll();
            request.setAttribute("listPenilaian", listPenilaian);
            List listNilaiKriteria = nilaiKriteriaService.loadAll();
            request.setAttribute(SISConstants.LIST_NILAI_KRITERIA, listNilaiKriteria);
            List listAccountKelas = accountKelasService.loadAll();
            request.setAttribute(SISConstants.LIST_ACCOUNT_KELAS, listAccountKelas);
            List listMataPelajaran = mataPelajaranService.loadAll();
            request.setAttribute(SISConstants.LIST_MATA_PELAJARAN, listMataPelajaran);
        } catch (Exception ex) {
            Logger.getLogger(NilaiAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.PENILAIAN_LIST);
    }

    public ActionForward penilaianAdd(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        PenilaianService penilaianService = (PenilaianService) CallCtx.getContext(request).getBean(SISBeanConstants.PENILAIAN_SERVICE);
        NilaiKriteriaService nilaiKriteriaService = (NilaiKriteriaService) CallCtx.getContext(request).getBean(SISBeanConstants.NILAI_KRITERIA_SERVICE);
        AccountKelasService accountKelasService = (AccountKelasService) CallCtx.getContext(request).getBean(SISBeanConstants.ACCOUNT_KELAS_SERVICE);
        MataPelajaranService mataPelajaranService = (MataPelajaranService) CallCtx.getContext(request).getBean(SISBeanConstants.MATA_PELAJARAN_SERVICE);
        NilaiForm nilaiForm = (NilaiForm) form;
        HttpSession session = request.getSession(true);
        User user = (User) session.getAttribute(SISConstants.USER );
        try {
            AccountKelas accountKelas = accountKelasService.loadById(nilaiForm.getAccountKelas());
            Penilaian penilaian = new Penilaian();
            penilaian.setAccountKelas(accountKelas);
            penilaian.setNama(nilaiForm.getNama());
            penilaian.setNilai(nilaiForm.getNilai());
            penilaian.setCreatedBy(user.getNama());
            penilaian.setCreatedDt(new Date());
            penilaianService.savePenilaian(penilaian);
            nilaiForm.reset(mapping, request);
        } catch (Exception ex) {
            Logger.getLogger(NilaiAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.PENILAIN);
    }

    public ActionForward penilaianEdit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        PenilaianService penilaianService = (PenilaianService) CallCtx.getContext(request).getBean(SISBeanConstants.PENILAIAN_SERVICE);
        NilaiKriteriaService nilaiKriteriaService = (NilaiKriteriaService) CallCtx.getContext(request).getBean(SISBeanConstants.NILAI_KRITERIA_SERVICE);
        AccountKelasService accountKelasService = (AccountKelasService) CallCtx.getContext(request).getBean(SISBeanConstants.ACCOUNT_KELAS_SERVICE);
        MataPelajaranService mataPelajaranService = (MataPelajaranService) CallCtx.getContext(request).getBean(SISBeanConstants.MATA_PELAJARAN_SERVICE);
        NilaiForm nilaiForm = (NilaiForm) form;
        try {
            Penilaian penilaian = penilaianService.loadById(Long.parseLong(request.getParameter(SISConstants.ID)));
            AccountKelas accountKelas = accountKelasService.loadById(penilaian.getAccountKelas().getId());
            nilaiForm.setAccountKelas(accountKelas.getId());
            nilaiForm.setNama(penilaian.getNama());
            nilaiForm.setNilai(penilaian.getNilai());
            List listNilaiKriteria = nilaiKriteriaService.loadAll();
            request.setAttribute(SISConstants.LIST_NILAI_KRITERIA, listNilaiKriteria);
            List listAccountKelas = accountKelasService.loadAll();
            request.setAttribute(SISConstants.LIST_ACCOUNT_KELAS, listAccountKelas);
            List listMataPelajaran = mataPelajaranService.loadAll();
            request.setAttribute(SISConstants.LIST_MATA_PELAJARAN, listMataPelajaran);
        } catch (Exception ex) {
            Logger.getLogger(NilaiAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.PENILAIAN_EDIT);
    }

    public ActionForward penilaianUpdate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        PenilaianService penilaianService = (PenilaianService) CallCtx.getContext(request).getBean(SISBeanConstants.PENILAIAN_SERVICE);
        NilaiKriteriaService nilaiKriteriaService = (NilaiKriteriaService) CallCtx.getContext(request).getBean(SISBeanConstants.NILAI_KRITERIA_SERVICE);
        AccountKelasService accountKelasService = (AccountKelasService) CallCtx.getContext(request).getBean(SISBeanConstants.ACCOUNT_KELAS_SERVICE);
        MataPelajaranService mataPelajaranService = (MataPelajaranService) CallCtx.getContext(request).getBean(SISBeanConstants.MATA_PELAJARAN_SERVICE);
        NilaiForm nilaiForm = (NilaiForm) form;
        HttpSession session = request.getSession(true);
        User user = (User) session.getAttribute(SISConstants.USER );
        try {
            AccountKelas accountKelas = accountKelasService.loadById(nilaiForm.getAccountKelas());
            Penilaian penilaian = penilaianService.loadById(nilaiForm.getId());
            penilaian.setAccountKelas(accountKelas);
            penilaian.setNama(nilaiForm.getNama());
            penilaian.setNilai(nilaiForm.getNilai());
            penilaian.setCreatedBy(user.getNama());
            penilaian.setCreatedDt(new Date());
            penilaianService.updatePenilaian(penilaian);
            nilaiForm.reset(mapping, request);
        } catch (Exception ex) {
            Logger.getLogger(NilaiAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.PENILAIN);
    }

    public ActionForward penilaianDelete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        PenilaianService penilaianService = (PenilaianService) CallCtx.getContext(request).getBean(SISBeanConstants.PENILAIAN_SERVICE);
        try {
            Penilaian penilaian = penilaianService.loadById(Long.parseLong(request.getParameter(SISConstants.ID)));
            penilaianService.deletePenilaian(penilaian);
        } catch (Exception ex) {
            Logger.getLogger(NilaiAction.class.getName()).log(Level.SEVERE, null, ex);
        }
        return mapping.findForward(SISScreenConstants.PENILAIN);
    }
}